<template>
  <div class="app-container">
    <div class="content-container">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="name" label="名称">
            <el-input v-model="queryParams.name" placeholder="请输入名称" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="handleSearch">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="mb-10px">
          <el-button type="success" icon="plus" @click="handleOpenDialog">新增</el-button>
        </div>
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            @handle-query="handlePageChange"
          >
            <template #state="{ row, name }">
              <el-tag :type="ACTIVITY_STATUS_MAP[row.state] || 'info'">
                {{ getLabelByValue(ACTIVITY_STATUS_ENUM, row.state) }}
              </el-tag>
            </template>
            <template #images="{ row, name }">
              <image-preview
                v-for="file in JSON.parse(row.images || '[]')"
                width="100px"
                height="100px"
                :src="file.url"
              ></image-preview>
            </template>
            <template #operation="{ row, name }">
              <el-button
                link
                type="primary"
                icon="Edit"
                :disabled="
                  [
                    ACTIVITY_STATUS_ENUM.ONGOING.value,
                    ACTIVITY_STATUS_ENUM.COMPLETED.value,
                  ].includes(row.state)
                "
                @click="handleEdit(row)"
              >
                重新提交
              </el-button>
              <el-button
                type="danger"
                link
                icon="delete"
                :disabled="row.state === ACTIVITY_STATUS_ENUM.ONGOING.value"
                @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <activity-form ref="activityFormRef" @load-data="handleQuery"></activity-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import { getLabelByValue } from "@/utils/enums";
import ActivityAPI, {
  SysMerchantActivityQuery,
  SysMerchantActivityVo,
} from "@/api/operationManage/activity";
import ActivityForm from "@/views/operationManage/activity/components/activity-form.vue";
import { ACTIVITY_STATUS_ENUM, ACTIVITY_STATUS_MAP } from "@/enums/operationManage/ActivityEnum";
import ImagePreview from "@/components/ImagePreview/index.vue";

const loading = ref(false);
const activityFormRef = ref();
const total = ref(0);
const queryFormRef = ref();

const queryParams = reactive<SysMerchantActivityQuery>({
  name: "",
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref<SysMerchantActivityVo[]>([]);
const tableColumnsData = ref([
  {
    prop: "name",
    label: "名称",
    showOverflowTooltip: true,
  },
  {
    prop: "title",
    label: "副标题",
    showOverflowTooltip: true,
  },
  {
    prop: "content",
    label: "活动内容",
    showOverflowTooltip: true,
  },
  {
    customSlot: "images",
    label: "活动图片",
    showOverflowTooltip: true,
  },
  {
    prop: "startTime",
    label: "开始时间",
    width: 180,
    showOverflowTooltip: true,
  },
  {
    prop: "endTime",
    label: "结束时间",
    width: 180,
    showOverflowTooltip: true,
  },
  {
    prop: "state",
    label: "活动状态",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "state",
  },
  {
    prop: "remark",
    label: "备注信息",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    prop: "operation",
    label: "操作",
    width: 200,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);
// 查询
function handleQuery() {
  loading.value = true;
  ActivityAPI.page(queryParams)
    .then((data) => {
      tableData.value = data.list || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

async function handleDelete(id: number) {
  const result = await ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });
  if (!result) return;
  try {
    loading.value = true;
    await ActivityAPI.deleteById({
      id,
    });
    ElMessage.success("删除成功");
    handleQuery();
  } catch (e) {
    console.log(e);
  } finally {
    loading.value = false;
  }
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.pageSize = 10;
  handleQuery();
}
// 查询
function handleSearch() {
  queryParams.pageNum = 1;
  handleQuery();
}

function handleOpenDialog() {
  activityFormRef.value.showModal();
}
function handleEdit(val) {
  activityFormRef.value.showModal(val);
}

function handlePageChange({ pageNum, pageSize }) {
  queryParams.pageNum = pageNum;
  queryParams.pageSize = pageSize;
  handleQuery();
}
onMounted(async () => {
  handleQuery();
});
</script>
